package pdf.myExample;

import org.apache.pdfbox.Loader;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PageMode;
import org.apache.pdfbox.pdmodel.common.PDMetadata;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.destination.PDDestination;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.destination.PDPageDestination;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.destination.PDPageFitWidthDestination;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDDocumentOutline;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDOutlineItem;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDOutlineNode;
import org.apache.pdfbox.rendering.PDFRenderer;

import java.io.File;
import java.io.IOException;

/**
 * 读取pdf的前10%，并且获取相关目录，写到另一个文件
 * @author ykh
 */
public class ReadPdf {
    public static void main(String[] args) throws IOException {

        try (
                PDDocument document = Loader.loadPDF(new File("D:\\IDEA\\ptp-finance\\generator\\src\\main\\java\\pde" +
                        "\\myExample\\MySQL是怎样运行的：从根儿上理解MySQL.pdf"));
                PDDocument save = new PDDocument();
        ) {
            final int numberOfPages = document.getNumberOfPages();
            final PDDocumentOutline documentOutline = document.getDocumentCatalog().getDocumentOutline();
            final PDDocumentOutline save_DocumentOutline = new PDDocumentOutline();

            if (documentOutline != null){
                PDOutlineItem current = documentOutline.getFirstChild();
                int i = 0;
                e: while (current != null){
                    PDOutlineItem priorCurrent = current;
                    final PDPage priorPage = priorCurrent.findDestinationPage(document);
                    current = current.getNextSibling();
                    PDPage destinationPage = null;
                    if (current != null) {
                        destinationPage = current.findDestinationPage(document);
                    }

                    boolean flag = false;
                    for (; i < Math.ceil(numberOfPages * 0.1); i++) {
                        final PDPage page = document.getPage(i);
                        if(priorPage.equals(page)){
                            final PDOutlineItem pdOutlineItem = new PDOutlineItem();
                            pdOutlineItem.setTitle(priorCurrent.getTitle());
                            pdOutlineItem.setDestination(priorCurrent.getDestination());//目标page
                            pdOutlineItem.setAction(priorCurrent.getAction());//动作（包括点击后的跳转动作）
                            save_DocumentOutline.addLast(pdOutlineItem);
                            flag = true;
                        }
                        if(!page.equals(destinationPage)){
                            save.addPage(page);
                        }else{
                            if(!flag){
                                throw new RuntimeException("目录是乱序的");
                            }
                            continue e;
                        }
                    }
                    current = null;
                }
                save.getDocumentCatalog().setDocumentOutline(save_DocumentOutline);
                save.getDocumentCatalog().setPageMode(PageMode.USE_OUTLINES);//打开时默认显示大纲
            }else {
                for (int i = 0; i < Math.ceil(numberOfPages * 0.1); i++) {
                    final PDPage page = document.getPage(i);
                    save.addPage(page);
                }
            }

            save.save("D:\\IDEA\\ptp-finance\\generator\\src\\main\\java\\pde" +
            "\\myExample\\mysql.pdf");
        }
    }
}
